initUsers();
function initUsers() {
    $.ajax({
        url: '/user.do',
        method: 'get',
        success: function (result) {
            if (result && result.length > 0) {
                for (var i = 0; i < result.length; i++) {
                    var val = result[i];
                    $(`<tr>
                    <td>${val.id}</td>
                    <td>${val.username}</td>
                    <td>${val.phone}</td>
                    <td>${val.state == 1 ? '正常' : '冻结'}</td>
                    <td style= "width:20%">
                    <button myId ="${val.id}" class="resetPass am-btn am-btn-success" style="border-radius:7%;box-shadow: rgb(75, 72, 72) 1px 1px 4px; font-size: 14px; margin:5px 5px">重置密码</button>
                    ${val.state == 1 ? `<button class="state_change am-btn am-btn-danger" myId ="${val.id}" style="border-radius:7%;box-shadow: rgb(75, 72, 72) 1px 1px 4px; font-size: 14px; margin:5px 5px">冻结</button>` : `<button myId ="${val.id}" class="am-btn am-btn-success state_change" style="border-radius:7%;box-shadow: rgb(75, 72, 72) 1px 1px 4px; font-size: 14px; margin:5px 5px">解冻</button>`}
                    </td>
                    </tr>`).appendTo('tbody');
                }
            }
        }
    });
}
//重置密码
$(document).on('click', '.resetPass', function () {
    //attr() 方法设置或返回被选元素的属性值。
    let id = $(this).attr('myId');
    swal({
        allowOutsideClick: false,
        showCancelButton: true,
        cancelButtonColor: '#d33',
        title: '请输入新密码',
        input: 'password',
        // showCancelButton: true,
        inputValidator: function (value) {
            // console.log(value);
            return new Promise(function (resolve, reject) {
                if (value) {
                    resolve();
                } else {
                    swal('密码不能为空','输入密码','error');
                }
            })
        }
    }).then(function (result) {
        // console.log(result.value);
        if (!result.value) {return;}
        //Ajax
        $.ajax({
            url: '/resetPass.do',
            method: 'post',
            data: `id=${id}&password=${result.value}`,
            success: function (result) {
                // console.log(result);
                if (result === 'success') {
                    swal('修改成功', '密码重置成功', 'success');
                } else {
                    swal('修改失败', '密码重置失败', 'error');
                }
            }
        });
    })

})
//修改状态
$(document).on('click', '.state_change', function () {
    let txt = $(this).html();
    let id = $(this).attr('myId');
    let state = txt == '冻结' ? 0 : 1;
    swal({
        title: '确定要' + txt + '吗？',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: '确定',
    }).then(function (resolve) {
        // console.log(resolve.value);
        if (!resolve.value) { return; }
        //Ajax
        $.ajax({
            url: '/change_state.do',
            method: 'POST',
            data: `id=${id}&state=${state}`,
            success: function (result) {
                if (result == 'success') {
                    location.reload();
                } else {
                    swal(txt + '失败', '后台繁忙，请稍后再试', 'error');
                }
            }
        })
    })
})

//添加用户
$('.add_user').click(function () {
    swal({
        allowOutsideClick: false,
        showCancelButton: true,
        cancelButtonColor: '#d33',
        title: '添加用户',
        html: `<input id="username" class="swal2-input" placeholder="请输入用户名"> <input id="password" class="swal2-input" placeholder="请输入密码"><input id="phone" class="swal2-input" placeholder="请输入手机号">`,
        preConfirm: function () {
            return new Promise(function (resolve) {
                resolve([
                    $('#username').val(),
                    $('#phone').val(),
                    $('#password').val()
                ])
            })
        },
        onOpen: function () {
            $('#username').focus()
        }
    }).then(function (result) {
        if (!result.value) { return; }
        // console.log(result.value);
        $.ajax({
            url: '/addUser.do',
            method: 'post',
            data: `username=${$('#username').val()}&password=${$('#password').val()}&phone=${$('#phone').val()}`,
            success: function (result) {
                // console.log(result);
                if (result == 'success') {
                    location.reload();
                } else {
                    swal('请完善添加信息', '添加失败', 'error');
                }
            }
        });
        // swal(JSON.stringify(result))
    }).catch(swal.noop)
});
